package com.x.erp.dao;

import com.x.erp.model.InsuranceOrder;
import com.x.erp.util.ConstantState;
import com.x.framework.dao.BaseDao;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * 延保商品订单dao
 *
 * @author pansong
 */
@Repository
public class InsuranceOrderDao extends BaseDao {

	/**
	 * 获取延保商品订单列表
	 *
	 * @param insuranceOrder
	 * @return
	 */
	private final static String selectInsuranceOrderListSql = "SELECT "
			+ "eio.*, "
			+ "em.market_name, "
			+ "es.shop_name, "
			+ "eu.user_name, "
			+ "eu.user_mobile "
			+ "FROM ERP_INSURANCE_ORDER eio "
			+ "LEFT JOIN ERP_MARKET em ON eio.MARKET_ID=em.MARKET_ID "
			+ "LEFT JOIN ERP_SHOP es ON eio.SHOP_ID=es.SHOP_ID "
			+ "LEFT JOIN ERP_USER eu ON eio.USER_ID=eu.USER_ID "
			+ "WHERE 1 = 1 ";

	public List<InsuranceOrder> selectInsuranceOrderList(InsuranceOrder insuranceOrder) throws Exception {
		StringBuilder sql = new StringBuilder(selectInsuranceOrderListSql);
		List<Object> params = new ArrayList<>();
		selectSearch(sql, params, insuranceOrder);
		// 已完成&失效的订单沉底，剩余数据按创建时间倒叙排列。
		sql.append("ORDER BY eio.ORDER_STATE, eio.ORDER_CREATE_TIME DESC, eio.ORDER_ID ");
		return this.queryForList(sql.toString(), InsuranceOrder.class, params, insuranceOrder);
	}

	/**
	 * 获取延保商品订单数量
	 *
	 * @param insuranceOrder
	 * @return
	 */
	private final static String selectInsuranceOrderNumSql = "SELECT "
			+ "COUNT(1) "
			+ "FROM ERP_INSURANCE_ORDER eio "
			+ "LEFT JOIN ERP_MARKET em ON eio.MARKET_ID=em.MARKET_ID "
			+ "LEFT JOIN ERP_SHOP es ON eio.SHOP_ID=es.SHOP_ID "
			+ "LEFT JOIN ERP_USER eu ON eio.USER_ID=eu.USER_ID "
			+ "WHERE 1 = 1 ";

	public int selectInsuranceOrderNum(InsuranceOrder insuranceOrder) throws Exception {
		StringBuilder sql = new StringBuilder(selectInsuranceOrderNumSql);
		List<Object> params = new ArrayList<>();
		selectSearch(sql, params, insuranceOrder);
		return this.queryForInt(sql.toString(), params);
	}

	/**
	 * 获取单个延保商品订单信息（带合同信息）
	 *
	 * @param insuranceOrder
	 * @return
	 */
	private final static String selectInsuranceOrderSql = "SELECT "
			+ "eio.*, "
			+ "eic.CONTRACT_NO, "
			+ "eic.CONTRACT_RECEIPT_NUM, "
			+ "eic.CONTRACT_IMAGE_URL "
			+ "FROM ERP_INSURANCE_ORDER eio "
			+ "LEFT JOIN ERP_INSURANCE_CONTRACT eic ON eio.CONTRACT_ID=eic.CONTRACT_ID "
			+ "WHERE 1 = 1 ";

	public InsuranceOrder selectInsuranceOrder(InsuranceOrder insuranceOrder) throws Exception {
		StringBuilder sql = new StringBuilder(selectInsuranceOrderSql);
		List<Object> params = new ArrayList<>();
		selectSearch(sql, params, insuranceOrder);
		sql.append("LIMIT 1 ");
		List<InsuranceOrder> goodsList = this.queryForList(sql.toString(), InsuranceOrder.class, params);
		if (goodsList != null && goodsList.size() > 0) {
			return goodsList.get(0);
		}
		return null;
	}

	/**
	 * 拼接查询条件
	 */
	private void selectSearch(StringBuilder sql, List<Object> params, InsuranceOrder insuranceOrder) {
		if (this.isNotNull(insuranceOrder.getOrderId())) {
			sql.append("AND eio.ORDER_ID = ? ");
			params.add(insuranceOrder.getOrderId());
		}
		// 订单编号模糊匹配
		if (this.isNotNull(insuranceOrder.getOrderNo())) {
			sql.append("AND eio.ORDER_NO LIKE ? ");
			params.add("%" + insuranceOrder.getOrderNo() + "%");
		}
		if (this.isNotNull(insuranceOrder.getContractId())) {
			sql.append("AND eio.CONTRACT_ID = ? ");
			params.add(insuranceOrder.getContractId());
		}
		if (this.isNotNull(insuranceOrder.getMarketId())) {
			sql.append("AND eio.MARKET_ID = ? ");
			params.add(insuranceOrder.getMarketId());
		}
		if (this.isNotNull(insuranceOrder.getShopId())) {
			sql.append("AND eio.SHOP_ID = ? ");
			params.add(insuranceOrder.getShopId());
		}
		if (this.isNotNull(insuranceOrder.getUserId())) {
			sql.append("AND eio.USER_ID = ? ");
			params.add(insuranceOrder.getUserId());
		}
		if (this.isNotNull(insuranceOrder.getOrderPrice())) {
			sql.append("AND eio.ORDER_PRICE = ? ");
			params.add(insuranceOrder.getOrderPrice());
		}
		if (this.isNotNull(insuranceOrder.getOrderState())) {
			// 状态值传40 获取使用中和已完成的
			if(insuranceOrder.getOrderState() == ConstantState.INSURANCE_ORDER_STATE_VERIFICATION){
				sql.append("AND eio.ORDER_STATE IN(?, ?) ");
				params.add(ConstantState.INSURANCE_ORDER_STATE_USING);
				params.add(ConstantState.INSURANCE_ORDER_STATE_FINISH);
			} else {
				sql.append("AND eio.ORDER_STATE = ? ");
				params.add(insuranceOrder.getOrderState());
			}
		}
	}
}
